/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import index.Element;
import index.ElementHandler;
import index.FullWord;
import index.Word;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.xpath.XPathExpressionException;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import sql.SQLInterface;

/**
 *
 * @author Lu
 */
public class TestOnOneDoc {

    public static void main(String[] args) throws XPathExpressionException {
        SAXParserFactory fabrique = SAXParserFactory.newInstance();
        SAXParser parseur;
        try {
            parseur = fabrique.newSAXParser();
            DefaultHandler gestionnaire = new ElementHandler();


            SQLInterface.init();
            String[] tables = new String[6];
            tables[5] = "elementtype";
            tables[4] = "document";
            tables[3] = "element";
            tables[2] = "word";
            tables[1] = "wordposition";
            tables[0] = "statdocelement";
            SQLInterface.cleanDatabase(tables);
            File fichier = new File("..\\Collection\\d002.xml");
            ((ElementHandler) gestionnaire).initNewFile(fichier);
            parseur.parse(fichier, gestionnaire);

            SQLInterface.addDocument(((ElementHandler) gestionnaire).getDocumentName());
            for (String elementType : ((ElementHandler) gestionnaire).getTypeList()) {
                SQLInterface.addElementType(elementType);
            }
            for (Element element : ((ElementHandler) gestionnaire).getElementList()) {
                SQLInterface.addElement(element.getXPath(), element.getType(), element.getDocument());
            }

            for (Word word : ((ElementHandler) gestionnaire).getWordIndex()) {
                SQLInterface.addWord(word.getName(), word.getCounter(), word.getFrequency(), word.getWeight());


                for (FullWord fullWord : word.getOccurences()) {
                    try {
                        SQLInterface.addFullWord(word.getName(), fullWord.getFullName(), fullWord.getDocument(),fullWord.getElement(), fullWord.getPosition());
                    } catch (SQLException ex) {
                        System.out.println(word.getName()+" "+ fullWord.getFullName()+" "+  fullWord.getDocument()+" "+ fullWord.getElement()+" "+ fullWord.getPosition());
                        Logger.getLogger(TestOnOneDoc.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }

        } catch (ParserConfigurationException ex) {
            Logger.getLogger(TestElementHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(TestElementHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TestElementHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(TestOnOneDoc.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
